package su.mygradleapps.decimalcalculator;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.IntegerField;
import com.vaadin.flow.router.Route;

/**
 * The main view
 */
@Route("/")
public class MainView extends VerticalLayout {

    public MainView(CalculatorService calculatorService) {
        IntegerField firstNumberField = new IntegerField();
        firstNumberField.setValue(0);
        firstNumberField.setStepButtonsVisible(true);
        firstNumberField.setLabel("First number:");

        IntegerField secondNumberField = new IntegerField();
        secondNumberField.setValue(0);
        secondNumberField.setStepButtonsVisible(true);
        secondNumberField.setLabel("Second number:");

        IntegerField resultField = new IntegerField();
        resultField.setValue(0);
        resultField.setReadOnly(true);
        resultField.setLabel("Result:");

        Div buttonsTopDiv = new Div();
        Div buttonsBottomDiv = new Div();
        Button plusButton = new Button("+");
        plusButton.addClickListener(event -> resultField.setValue(calculatorService.plusOperation(
                firstNumberField.getValue(), secondNumberField.getValue())));
        Button minusButton = new Button("-");
        minusButton.addClickListener(event -> resultField.setValue(calculatorService.minusOperation(
                firstNumberField.getValue(), secondNumberField.getValue())));
        Button multiplyButton = new Button("*");
        multiplyButton.addClickListener(event -> resultField.setValue(calculatorService.multiplyOperation(
                firstNumberField.getValue(), secondNumberField.getValue())));
        Button divideButton = new Button("/");
        divideButton.addClickListener(event -> resultField.setValue(calculatorService.divideOperation(
                firstNumberField.getValue(), secondNumberField.getValue())));
        buttonsTopDiv.add(plusButton);
        buttonsTopDiv.add(minusButton);
        buttonsBottomDiv.add(multiplyButton);
        buttonsBottomDiv.add(divideButton);

        add(new H2("Simple calculator"));
        add(firstNumberField);
        add(secondNumberField);
        add(buttonsTopDiv);
        add(buttonsBottomDiv);
        add(resultField);
    }

}
